<?php namespace App\Controllers\Admin;

// =====================================================================
// CONTENT METHODS
// =====================================================================

class ContentsController extends AdminController {
   
    // ---------------------------------------------------------------------        
    // postUserabm, is the function to information of users.
    // ---------------------------------------------------------------------        
    public function index() {
        $this->layout->content = \View::make('admin.contents.index')->with('contents', \Contents::all());
    }
    
    // ---------------------------------------------------------------------        
    // postUserabm, is the function to information of users.
    // ---------------------------------------------------------------------        
    public function create() {
        $this->layout->content = \View::make('admin.contents.abm')->withCategories(\Categories::all());
    }
   
    // ---------------------------------------------------------------------        
    // postUserabm, is the function to information of users.
    // ---------------------------------------------------------------------            
    public function store() {
            
        // Require some validation
	    $rules = array(
	        'title' => 'required'
	    );
	    
        // Get the submitted data
	    $input = \Input::get();

        // Validate
	    $validation = \Validator::make($input, $rules);
	    if ($validation->fails()) {
	        return \Redirect::to('admin/create')->withInput()->withErrors($validation);
	    }
        
        //Create user
	    $content = \Contents::create($input);    
	    $content->active = \Input::get('active') == 'Y' ? 1 : 0;      
	    $content->save();
        
		return \Redirect::to('admin/contents')->with('success','Content has been added successfully.');
	}
	
    // ---------------------------------------------------------------------        
    // getUseredit, is the function to show the user form to edit.
    // ---------------------------------------------------------------------        
    public function edit($id=null) {
    
        $galleryItems = null;
        
        $content = \Contents::find($id);
        $gallery = \ContentsGallery::where("contentID","=",$content->contentID)->first();

        if ($gallery) {
             // esperamos a qeu cree los items para descomentar
            $gallery = \Galleries::find($gallery->id);
            if ($gallery != null) {            
                $response['success'] = false;
                $galleryItemsOBJ = \GalleriesItems::where('galleryID','=',$gallery->id)->orderBy('order')->get();
                foreach($galleryItemsOBJ as $key => $item)
                {
                    switch($item->type)
                    {
                        case 'image':
                            $image =  \Images::find($item->itemID);                         
                            if ($image != null) {            
                                $galleryItems[] = array("itemID"    => $item->id,
                                                        "type"      => $item->type,
                                                        "to_show"   => "<img src='".$image->url()."' height='100%'>");
                            }   
                            break;
                        case 'video':
                            $video =  \Videos::find($item->itemID);
                            if ($video != null) {            
                                $galleryItems[] = array("itemID"    => $item->id,
                                                        'type'      => $item->type,
                                                        'to_show'   => preg_replace('/(width|height)(=)"([\d]+)"/', '${1}${2}"100%"', $video->embed_code));
                            }   
                            break;
                    }
                }
            }
            
        }
        
        $this->layout->content = \View::make('admin.contents.edit')
                                ->with(array('content'=> $content, 'gallery' => $gallery, 'galleryItems'=>$galleryItems))
                                ->withCategories(\Categories::all());
    }

    // ---------------------------------------------------------------------        
    // postUseredit, is the function to edit users.
    // ---------------------------------------------------------------------            
    public function update($id) {
      
        try {
        
			$content = \Contents::find($id);
			
			$content->title = \Input::get('title');
			$content->categoryID = \Input::get('categoryID');
			$content->description = \Input::get('description');
			$content->content = \Input::get('content');
			$content->author = \Input::get('author');
			$content->active = \Input::get('active') ? 'Y' : 'N';
			$content->html_meta_title = \Input::get('html_meta_title');
			$content->html_meta_description = \Input::get('html_meta_description');
			$content->html_meta_keywords = \Input::get('html_meta_keywords');
			
			if($content->save()) {
				return \Redirect::to('admin/contents')->with('success','Content has been updated successfully');
			} else {
				return \Redirect::to('admin/contents')->with('error','Unable to save content');
			}
		}
		catch (\Cartalyst\Sentry\Users\UserNotFoundException $e) {
		    return \Redirect::to('admin/contents')->with('error','Unable to find content');
		}
	}
	
    // ---------------------------------------------------------------------        
    // postContentdelete, is the function to delete content.
    // ---------------------------------------------------------------------        
	public function destroy($id=null) {
	       
		try {
	 		$content = \Contents::find($id);
			$delete = $content->delete();

		    if ($delete) {
		        return \Redirect::to('admin/contents')->with('success', 'Content has been deleted successfully.');
		    }
		    else {
		        return \Redirect::to('admin/contents')->with('error',"Content not deleted, please try again");
		    }
		}
		catch (\Cartalyst\Sentry\Users\UserNotFoundException $e) {
		    return \Redirect::to('admin/contents')->with('error','Unable to find content');
		}
	}
	    
    	// ---------------------------------------------------------------------        
    // postUserabm, is the function to information of users.
    // ---------------------------------------------------------------------        
    public function hasGallery() {
        return \Categories::all();
    }
}

